﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using TheOliver.DigitalSignage.Admin.Controls;
using Web;
using Web.Services;

namespace TheOliver.DigitalSignage.Admin.Views
{
    public partial class Events : Page
    {
        
        public Events()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void eventDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void _addNew_Click(object sender, RoutedEventArgs e)
        {
            AddNewEvent();
        }

        private void _save_Click(object sender, RoutedEventArgs e)
        {
            eventDomainDataSource.SubmitChanges();
        }

        private void _reject_Click(object sender, RoutedEventArgs e)
        {
            eventDomainDataSource.RejectChanges();
        }

        private void _delete_Click(object sender, RoutedEventArgs e)
        {
            DeleteEventQuestion();
        }

        private void DeleteEventQuestion()
        {
            if (eventDataGrid.SelectedItem != null)
            {
                QuestionWindow qw = 
                    new QuestionWindow(TextResource.DeleteEventQuestion, DeleteEvent);
                qw.Show();
            }
        }

        private void DeleteEvent()
        {
            DigitalSignageDomainContext _context = 
                eventDomainDataSource.DomainContext as DigitalSignageDomainContext;

            Event ev = eventDataGrid.SelectedItem as Event;
            _context.Events.Remove(ev);
        }

        private void AddNewEvent()
        {
            Event ev = new Event();
            ev.StartDate = DateTime.Today;
            ev.Name = "New Event";
            ev.Description = "New Event";

            DigitalSignageDomainContext _context =
                eventDomainDataSource.DomainContext as DigitalSignageDomainContext;

            _context.Events.Add(ev);
        }

        private void eventDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            if (e.HasError)
            {
                MessageBox.Show(e.Error.Message);
                e.MarkErrorAsHandled();
            }
        }
    }
}
